<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="出借标题" prop="title">
						<el-input v-model="form.title" placeholder="出借标题" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="出借证照" prop="licId">
						<el-select v-model="form.licId" clearable filterable remote :remote-method="remoteMethod" style="width: 100%" placeholder="输入证照名称、编号">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
			<el-col :lg="12">
				<el-form-item label="借方单位" prop="orgName">
					<el-input v-model="form.orgName" placeholder="借方单位" clearable maxlength="80"></el-input>
				</el-form-item>
			</el-col>
				<el-col :lg="12">
					<el-form-item label="出借人员" prop="borrowingUser">
						<select-user-dialog v-model:accountId="form.borrowingUser" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="出借日期" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="出借日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="截止日期" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="截止日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="还归日期" prop="backTime">
						<el-date-picker v-model="form.backTime" placeholder="还归日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="归还人员" prop="backUser">
						<el-input v-model="form.backUser" placeholder="归还人员" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '添加出借记录',
				edit: '编辑出借记录',
				show: '查看'
			},
			options:[],
			form: {
				recordId: '',
				title: '',
				licId: '',
				orgName: '',
				borrowingUser: "",
				backTime:"",
				backUser: '',
				beginTime: '',
				endTime: '',
				remark: ''
			},
			rules: {
				borrowingUser: [
					{required: true, message: '出借人员不能为空'}
				],
				orgName: [
					{required: true, message: '借方单位不能为空'}
				],
				beginTime: [
					{required: true, message: '借出日期不能为空'}
				],
				endTime: [
					{required: true, message: '截至日期不能为空'}
				],
				title: [
					{required: true, message: '出借标题不能为空'}
				]
			}
		}
	},
	mounted() {
		this.remoteMethod("");
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.office.licenceRecord.getLicenceRecordListForSelect.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					if (this.mode == "add") {
						var res = await this.$API.office.licenceBorrowing.createBpmByLicenceRecordRule.post(this.form);
						this.isSaveing = false;
						if (res.code == 200) {
							this.$nextTick(() => {
								this.$router.replace({
									path: '/bpm/handle/dowork',
									query: {
										runId: res.data.runId,
										stepRunId: res.data.stepRunId,
										flowId: res.data.flowId
									}
								})
								this.visible = false;
								this.$message.success("操作成功")
							})
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else {
						var res = await this.$API.office.licenceBorrowing.updateLicenceBorrowing.post(this.form);
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.licId = data.licId
			this.form.orgName = data.orgName
			this.form.borrowingUser = data.borrowingUser
			this.form.backTime = data.backTime
			this.form.backUser = data.backUser
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
